/*
 * Location.java
 *
 * Copyright 2008 the Desert Labs Project (http://desertlabs.us)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package us.desertlabs.physics;

/**
 * Used to define a specific location in a specific <CODE>Space</CODE>.
 *
 * @author Brandon Franklin
 */
public class Location extends DimensionalValue {

    /** The multidimensional space in which this location exists */
    final private Space space;

    /**
     * Creates a new instance of <CODE>Location</CODE> at the specified
     * coordinates in the supplied <CODE>Space</CODE>.
     *
     * @param space the <CODE>Space</CODE> in which the location exists
     * @param coords the coordinates of the location in space
     * @throws IllegalArgumentException if any of the parameters are invalid
     */
    public Location( final Space space, final Number[] coords ) {

        super( coords );

        // Validate the Space
        if( space == null ) {
            throw new IllegalArgumentException( "Space cannot be null." );
        }
        this.space = space;

        // Validate the coordinates
        if( !isValid() ) {
            final DimensionalValue dimVal = new DimensionalValue( coords );
            final String dimStr = dimVal.toString();
            final String coordStr = dimStr.substring( dimStr.indexOf( "[" ) );
            throw new IllegalArgumentException( "Coordinates are not valid in the supplied Space: " + coordStr );
        }

    }

    /**
     * Returns the coordinates of this <CODE>Location</CODE> within its
     * defined <CODE>Space</CODE>.
     *
     * @return the coordinates of this <CODE>Location</CODE> within its
     *         defined <CODE>Space</CODE>
     */
    public Number[] getCoordinates() {
        return getValues();
    }

    /**
     * Returns the <CODE>Space</CODE> that is defined for this <CODE>Location</CODE>.
     *
     * @return the <CODE>Space</CODE> that is defined for this <CODE>Location</CODE>
     */
    public Space getSpace() {
        return space;
    }

    /**
     * Checks whether or not this <CODE>Location</CODE> is currently valid for
     * its defined <CODE>Space</CODE>, and returns true if so, false
     * otherwise.
     *
     * @return true if this <CODE>Location</CODE> is currently valid for its
     *         defined <CODE>Space</CODE>, false otherwise
     */
    @Override
    public boolean isValid() {
        boolean valid = super.isValid();
        if( valid ) {
            valid = space.checkCoordinates( getCoordinates() );
        }

        return valid;
    }
}
